<template>
	<view>
		<view class="speck">
			<view class="head">
				<view class="img">
					<view v-for="(item, i) in tempFilePaths" :key="i" style="position: relative;">
						<image :class="[(i+1)%4 === 0 ?'noMargin':'']" :src="item" @click="previewImage(i)" mode="">
						</image>
						<image :class="[(i+1)%4 === 0 ?'noMargindel':'']" class="del" src="@/others/static/del.png"
							mode="" @click="delImg(i)"></image>
					</view>
					<view class="upload" @click="uploadFile"></view>
				</view>
				<view class="inputWrap">
					<textarea
					  class="bg-body textarea"
					  style="width: 100%"
					  placeholder="展开说说对服务项目的体验吧，300字以内"
					  v-model="content"
					  name="textarea"
					></textarea>
				</view>
			</view>
		</view>
		<view class="say">
			<view class="title">评价</view>
			<view class="flex row-around">
				<u-rate @change="change" :count="5" :current="value" :size="60" :active-color="'#477EE3'"></u-rate>
			</view>
		</view>

		<view @click="submit" class="other-button" hover-class="other-button-hover">提交</view>
	</view>
</template>

<script>
	import { baseimgURL } from "@/config/app";
	import {
		uploadFile
	} from '@/utils/tools';
	import { orderPl } from '@/api/others.js';
	export default {
		data() {
			return {
				id: '',
				tempFilePaths: [
					// '@/static/mains/m1.png',
					// '@/static/mains/m1.png',
					// '@/static/mains/m1.png',
				],
				content: '',
				value: 5
			}
		},
		onLoad(options) {
			this.id = options.id;
		},
		methods: {
			change(e){
				console.log(e)
				this.value = e;
			},
			// 上传图片
			async uploadFile() {
				let that = this;
				const [error, success] = await uni.chooseImage({
					count: 5
				})
				if (error) {
					return
				}
				uni.showLoading({
					title: '上传中...'
				})
				try {
					that.tempFilePaths = [];
					success.tempFilePaths.forEach(async item=>{
						const file = await uploadFile(item)
						console.log(file)
						that.tempFilePaths.push(file.uri)
					})
					uni.hideLoading()
				} catch (e) {
					that.$toast({
						title: '上传失败，请稍后再试'
					})
					uni.hideLoading()
				}
			
			},
			// 删除
			delImg(i) {
				this.tempFilePaths.splice(i, 1);
			},
			// 预览
			previewImage: function(index) {
				let imgs = []
				for(let i=0;i<this.tempFilePaths.length;i++){
					let str = this.tempFilePaths[i]
					imgs.push(str)
				}
				uni.previewImage({
					current: index, //预览图片的下标
					urls: imgs //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
				})
			},
			async submit(){
				if (!this.content) return this.$toast({
				    title: '请输入您的体验'
				});
				let imgs = [];
				if(this.tempFilePaths.length>0){
					this.tempFilePaths.forEach(item=>{
						imgs.push(item.split(baseimgURL)[1])
					})
				}
				let params ={
					order_id:this.id,
					service_comment:this.value,
					comment:this.content,
					images:imgs.toString()
				}
				orderPl(params).then(res => {
					this.$toast({
						title: '操作成功'
					})
					setTimeout(function() {
						uni.navigateBack()
					}, 1000);
				});
			},
		}
	}
</script>

<style scoped lang="less">
	.speck {
		width: 100%;

		.head {
			padding:25rpx;
			box-sizing: border-box;
			.inputWrap{
				margin-top: 50rpx;
				background: #fff;
				border-radius: 25rpx;
		
				.textarea{
					width: 100%;
					background: #fff;
					padding: 30rpx;
					border-radius: 25rpx;
					border: none;
					box-sizing: border-box;
				}
			}
			.img {
				display: flex;
				flex-wrap: wrap;
				background: #fff;
				padding: 30rpx;
				border-radius: 25rpx;
				.del {
					width: 40rpx;
					height: 40rpx;
					position: absolute;
					top: -20rpx;
					right: -20rpx;
				}
			}
			
			image {
				width: 140rpx;
				height: 140rpx;
				margin-bottom: 20rpx;
				margin-right: 26rpx;
			}
			
			.noMargin {
				margin-right: 0rpx;
			}
			
			.noMargindel {
				top: -20rpx !important;
				right: -44rpx !important;
			}
			
			.upload {
				width: 140rpx;
				height: 140rpx;
				background: url(@/others/static/a7.png) no-repeat;
				background-size: 100% 100%;
				margin-bottom: 20rpx;
			}

		}
	}
	.say{
		background: #fff;
		padding: 30rpx;
		border-radius: 25rpx;
		margin:30rpx;
		
		.title{
			font-size: 30rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}
	}
</style>